import { MutableRefObject } from 'react';
import { extendApi } from '@inbiz/utils';
import type { IProps } from './index';

export default extendApi(
  (
    _events: MutableRefObject<IProps['callBackEvents']>,
    props: IProps,
    filed: any,
    options,
    setOptions,
  ) => {
    let nowValidator = filed.validator || [];
    return {
      // 设置输入框最大字数
      setMaxLength: (len: number) => {
        let flag = false;
        filed.validator?.map((item: any, index: number) => {
          if (Object.keys(item)[0] === 'maxLength') {
            flag = true;
            nowValidator[index]['maxLength'] = len;
          }
        });
        if (flag) {
          filed.setValidator(nowValidator);
        } else {
          nowValidator.push({ maxLength: len });
          filed.setValidator(nowValidator);
        }
      },
      // 设置输入框最小字数
      setMinLength: (len: number) => {
        let flag = false;
        filed.validator?.map((item: any, index: number) => {
          if (Object.keys(item)[0] === 'minLength') {
            flag = true;
            nowValidator[index]['minLength'] = len;
          }
        });
        if (flag) {
          filed.setValidator(nowValidator);
        } else {
          nowValidator.push({ minLength: len });
          filed.setValidator(nowValidator);
        }
      },
      loadData: (data: { value: string; label?: string }[]) => {
        setOptions(data);
      },
      getData: () => options,
    };
  },
);
